package com.andyage.bce;

import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.io.InputStream;
import java.net.URL;

import com.baidubce.services.bos.BosClient;
import com.baidubce.services.bos.model.BosObjectSummary;
import com.baidubce.services.bos.model.ListObjectsResponse;
import com.baidubce.services.bos.model.ObjectMetadata;
import com.baidubce.services.bos.model.PutObjectResponse;

public class UploadFile {
	/**
	 * 以文件形式上传Object
	 * @param client
	 * @param bucketName
	 * @param objectKey
	 * @throws FileNotFoundException
	 */
	public void fileUploadObject(BosClient client,String bucketName, String objectKey,String filePath) throws FileNotFoundException{
	    // 获取指定文件
	    File file = new File(filePath);
	    // 以文件形式上传Object
	    PutObjectResponse putObjectFromFileResponse = client.putObject(bucketName, objectKey, file);
	    // 打印ETag
	    System.out.println(putObjectFromFileResponse.getETag());
	}
	/**
	 * 以数据流形式上传
	 * @param client
	 * @param bucketName
	 * @param objectKey
	 * @throws IOException 
	 */
	public void PutObject(BosClient client,String bucketName,String objectKey,Object object) throws IOException {
	    // 获取数据流
		File file=new File("D://svn.zip");
	    InputStream inputStream = new FileInputStream(file);
		PutObjectResponse putObjectResponseFromInputStream = client.putObject(bucketName, objectKey, inputStream);
		System.out.println(putObjectResponseFromInputStream.getETag());
	}
	/**
	 *  以二进制串上传Object
	 * @param client
	 * @param bucketName
	 * @param objectKey
	 * @param byte1
	 */
	public  void PutObject(BosClient client,String bucketName,String objectKey,byte[] byte1) {
		PutObjectResponse putObjectResponseFromByte = client.putObject(bucketName, objectKey, byte1);
		System.out.println(putObjectResponseFromByte.getETag());
	}
	/**
	 * 以字符串上传Object
	 * @param client
	 * @param bucketName
	 * @param objectKey
	 * @param string1
	 */
	public  void PutObject(BosClient client,String bucketName,String objectKey,String string1) {
		  // 以字符串上传Object
	    PutObjectResponse putObjectResponseFromString = client.putObject(bucketName, objectKey, string1);
	    System.out.println(putObjectResponseFromString.getETag());
	}
	
	public  void setHttpHeader(BosClient client) {
		// TODO Auto-generated method stub
		ObjectMetadata meta = new ObjectMetadata();

		// 设置ContentLength大小
		meta.setContentLength(1000);

		// 设置ContentType
		meta.setContentType("application/json");

		client.putObject("bucketName", "objectKey", "content", meta);
	}
	/**
	 * 用户自定义元数据
	 * @param client
	 */
	public void userMeyadata(BosClient client) {
		ObjectMetadata meta = new ObjectMetadata();
		// 设置自定义元数据name的值为my-data
		meta.addUserMetadata("name", "my-data");

		// 上传Object
		client.putObject("bucketName", "objectKey", "content", meta);
	}
	/**
	 * 查看Bucket中Object列表
	 * @param client
	 * @param bucketName
	 */
	public void listObjects(BosClient client, String bucketName) {

	    // 获取指定Bucket下的所有Object信息
	    ListObjectsResponse listing = client.listObjects(bucketName);

	    // 遍历所有Object
	    for (BosObjectSummary objectSummary : listing.getContents()) {
	        System.out.println("ObjectKey: " + objectSummary.getKey());
	    }

	}
	public String generatePresignedUrl(BosClient client, String bucketName, String objectKey, int expirationInSeconds) {

		   URL url = client.generatePresignedUrl(bucketName, objectKey, expirationInSeconds);          

		   //指定用户需要获取的Object所在的Bucket名称、该Object名称、时间戳、URL的有效时长   
		   System.out.println(url);
		    return url.toString();
		}
	public static void main(String[] args) throws FileNotFoundException {
		UploadFile upload=new UploadFile();
		upload.listObjects(Sample.createBosClient(), "andytest");
		//upload.PutObject(Sample.createBosClient(), "andytest", "tupian.jpg");
		upload.fileUploadObject(Sample.createBosClient(), "andytest", "tupian.jpg","");
		upload.generatePresignedUrl(Sample.createBosClient(), "andytest", "tupian.jpg", -1);
	}
}
